package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhuandan/data_source"
	"zhuandan/models"
)

type Infouserxs struct {
	Data       []models.Useryyb `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}
type UserxslistBody struct {
	Page     int `json:"pageNo" from:"pageNo"`
	Pagesize int `json:"pageSize" from:"pageSize"`

	Laiyuan int64 `gorm:"column:laiyuan" json:"laiyuan"`
	Addtime int64 `gorm:"column:addtime" json:"addtime"`
	Endtime int64 `gorm:"column:endtime" json:"endtime"`
}

func Userxslist(ctx *gin.Context) {
	request := UserxslistBody{}
	ctx.ShouldBind(&request)
	laiyuan := request.Laiyuan
	addtime := request.Addtime
	endtime := request.Endtime

	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Useryyb
	res := data_source.Db
	if laiyuan > 0 {
		res = res.Where("laiyuan = ?", laiyuan)
	}
	if addtime > 0 {
		res = res.Where("addtime >= ?", addtime)
	}
	if endtime > 0 {
		endtime := endtime + 86400
		res = res.Where("addtime < ?", endtime+86400)
	}
	res.Model(&models.Useryyb{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := Infouserxs{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type LogxsRBody struct {
	Data       []models.Logxs `json:"data"`
	PageNo     int            `json:"pageNo"`
	PageSize   int            `json:"pageSize"`
	TotalCount int64          `json:"totalCount"`
	TotalPage  int64          `json:"totalPage"`
}
type LogxsBody struct {
	Page     int `json:"pageNo" from:"pageNo"`
	Pagesize int `json:"pageSize" from:"pageSize"`

	Uid int64 `gorm:"column:uid" json:"uid"`
}

func Logxs(ctx *gin.Context) {
	request := LogxsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid

	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Logxs
	res := data_source.Db
	if uid > 0 {
		res = res.Where("uid = ?", uid)
	}

	res.Model(&models.Logxs{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := LogxsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type XiansuoRBody struct {
	Data       []models.Xiansuo `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}

func Xiansuo(ctx *gin.Context) {
	request := LogxsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid

	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Xiansuo
	res := data_source.Db
	if uid > 0 {
		res = res.Where("uid = ?", uid)
	}

	res.Model(&models.Xiansuo{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := XiansuoRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type XiansuoaddBody struct {
	ID       int64   `json:"id" gorm:"id"`
	Name     string  `json:"name" gorm:"name"`
	Xiaohao  int64   `json:"xiaohao" gorm:"xiaohao"`
	Xiansuo  int64   `json:"xiansuo" gorm:"xiansuo"`
	Price    float64 `json:"price" gorm:"price"`
	Chengben float64 `json:"chengben" gorm:"chengben"`
	Laiyuan  string  `json:"laiyuan" gorm:"laiyuan"`
	Phone    string  `json:"phone" gorm:"phone"`
}

func Xiansuoadd(ctx *gin.Context) {
	request := XiansuoaddBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	xiaohao := request.Xiaohao
	xiansuo := request.Xiansuo
	price := request.Price
	chengben := request.Chengben
	laiyuan := request.Laiyuan
	phone := request.Phone

	xs := models.Xiansuo{
		Name:     name,
		Xiansuo:  xiansuo,
		Xiaohao:  xiaohao,
		Price:    price,
		Chengben: chengben,
		Laiyuan:  laiyuan,
		Phone:    phone,
	}
	data_source.Db.Create(&xs)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Xiansuodel(ctx *gin.Context) {
	request := XiansuoaddBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Xiansuo{})

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Fbrphonecl(ctx *gin.Context) {
	var procure []models.Procure
	data_source.Db.Find(&procure)
	for _, v := range procure {
		var user models.User
		data_source.Db.Where("id = ?", v.Uid).First(&user)

		data_source.Db.Model(&models.Procure{}).Where("id = ?", v.ID).Update("fbrphone", user.Phone)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
